<!--
  * 代理商表
  *
  * @Author:    zwd
  * @Date:      2025-01-23 04:08:26
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="公司简称" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.companyAbbrNameLeftLike" placeholder="请输入公司简称" />
      </a-form-item>
      <a-form-item label="联系人" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.nameLeftLike" placeholder="请输入联系人" />
      </a-form-item>
      <a-form-item label="代理商账号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.no" placeholder="输入账号" />
      </a-form-item>
      <a-form-item label="状态" class="smart-query-form-item">
        <a-select v-model:value="queryForm.status" style="width: 180px" allowClear :options="ZZB_NO_STATUS" placeholder="请选择状态" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <a-button @click="showForm" type="primary" size="small" v-privilege="'agent:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          新增
        </a-button>
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="showForm(record)" type="link" v-privilege="'agent:update'">修改</a-button>
            <a-button @click="resetPassword(record)" type="link" v-privilege="'agent:reset_password'">密码重置</a-button>
            <a-button @click="viewMerchant(record)" type="link" v-privilege="'agent:find_id'">查看商户</a-button>
            <a-button @click="setRate(record)" type="link" v-privilege="'agent:set_rates'">{{ record.level === 2 ? '查看' : '设置' }}费率</a-button>
          </div>
        </template>
      </template>
    </a-table>

    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>

    <AgentsForm ref="formRef" @reloadList="queryData" />
    <RateList ref="rateFormRef" @reloadList="queryData" />
    <ResetPwdModal ref="resetPwdRef" @success="queryData" :apiFunction="agentApi.resetPassword" />
    <MerchantList ref="merchantDetailRef" />
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message } from 'ant-design-vue'
  import { agentApi } from '/src/api/business/b-operation/agent/agent-api.js'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import AgentsForm from './agent-form.vue'
  import RateList from './rate-list.vue'
  import MerchantList from '/@/components/business/comment/merchant-detail-list.vue'
  import ResetPwdModal from '/@/components/business/comment/reset-pwd-modal.vue'
  import { selectDictLabel } from '/@/utils/common'
  import {
    ZZB_NO_STATUS, // 代理商状态
  } from '/@/constants/dict'
  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------

  const statusTypeColorArray = ['red', 'blue']

  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '登录账号',
      dataIndex: 'no',
      ellipsis: true,
    },
    // {
    // title: '代理商id',
    // dataIndex: 'id',
    // ellipsis: true,
    // },
    {
      title: '公司简称',
      dataIndex: 'companyAbbrName',
      ellipsis: true,
    },
    {
      title: '授权业务',
      dataIndex: 'authBussType',
      customRender: ({ text }) => {
        return selectDictLabel(proxy.$smartEnumPlugin.getValueDescList('AUTH_BUSINESS_ENUM'), text)
      },
      ellipsis: true,
    },
    {
      title: '联系人',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '联系方式',
      dataIndex: 'phone',
      ellipsis: true,
    },
    {
      title: '地址',
      dataIndex: 'addr',
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(ZZB_NO_STATUS, text)
      },
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 250,
    },
  ])
  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    companyAbbrNameLeftLike: undefined, //公司名称
    nameLeftLike: undefined, //联系人
    no: undefined, //代理商账号
    status: undefined, //状态·态，0正常使用，1禁止开代理商，2禁止开商户，3禁止操作人员登录，4系统停止使用，5停止全部收款，6全部禁用
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await agentApi.page(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }
  // 重置密码
  const resetPwdRef = ref()
  async function resetPassword(record) {
    if (!resetPwdRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    resetPwdRef.value.show(record)
  }

  // 查看商户
  const merchantDetailRef = ref()

  // 查看商户
  async function viewMerchant(record) {
    if (!merchantDetailRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    record.exhibitionId = undefined
    merchantDetailRef.value.show(record)
  }

  // 费率设置表单ref
  const rateFormRef = ref()

  // 设置费率
  async function setRate(record) {
    if (!rateFormRef.value) {
      message.error('组件未加载完成，请稍后重试')
      return
    }
    rateFormRef.value.show(record)
  }

  onMounted(queryData)

  // ---------------------------- 添加/修改 ----------------------------
  const formRef = ref()

  function showForm(data) {
    formRef.value.show(data)
  }
</script>
